//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<GameDebug.cpp>
///	@path	~/src/appear/xwin/
///	@date	2007/11/12
///	@desc	Game debug integration.

#include "config/config.h"

#include "lib/system/redirect.h"

#include "appear/xwin/GameDebug.h"

namespace xeres {

	// GameDebug
	GameDebug::GameDebug( void )
		: m_activedConsole( NULL )
	{
#ifndef _FINAL
		// redirect
//		redirect_stdio( _S("xeres_debug") ,
//			trace_stdout , trace_stderr );
#endif
	}

	// ~GameDebug
	GameDebug::~GameDebug( void )
	{
#ifndef _FINAL
//		reset_redirect();
#endif
	}

	// Initialize
	void GameDebug::Initialize( void )
	{

	}

	// Finalize
	void GameDebug::Finalize( void )
	{
		dict<WString,Console*>::iterator it = m_consoles.begin();
		while( it!= m_consoles.end() )
		{
			(it->second)->Finalize();
			delete (it->second);
			++it;
		}
		m_consoles.clear();
	}

	// Tick
	void GameDebug::Tick( Engine& engine )
	{
		if( m_activedConsole )
		{
			m_activedConsole->Tick( engine );
		}
	}

	// OnKeyInput
	HandleInput GameDebug::OnKeyInput( const KeyInput& input )
	{
		// handles key input
		if( m_activedConsole )
		{
			if( input.GetKey() == KeyInput::KEY_ESCAPE )
			{
				m_activedConsole->OnActive( false );
				m_activedConsole = NULL;
			}
			else if( m_activedConsole->OnKeyInput( input ) == INPUT_HANDLED )
				return INPUT_HANDLED;
		}

		// continue
		return INPUT_UNHANDLED;
	}

	// OnMouseInput
	HandleInput GameDebug::OnMouseInput( const MouseInput& input )
	{
		// handles mouse input
		if( m_activedConsole )
		{
			if( m_activedConsole->OnMouseInput( input ) == INPUT_HANDLED )
				return INPUT_HANDLED;
		}

		// continue
		return INPUT_UNHANDLED;
	}

	// Render
	void GameDebug::Render( RenderContext& rc )
	{
		if( m_activedConsole )
		{
			m_activedConsole->Render( rc );
		}
	}

	// RegisterConsole
	bool GameDebug::RegisterConsole( const WString& name , Console * console )
	{
		dict<WString,Console*>::iterator it = m_consoles.begin();
		while( it!= m_consoles.end() )
		{
			if( it->first == name )
			{
				TRACE_ERROR( _S("GameDebug::RegisterConsole: Console '%s' has been registered.") , name.c_str() );
				return false;
			}
			if( it->second == console )
			{
				TRACE_ERROR( _S("GameDebug::RegisterConsole: Console cannot be registered twice \
							  (first as '%s' now used in '%s').") ,
					it->first.c_str() , name.c_str() );
				return false;
			}
			++it;
		}
		m_consoles[ name ] = console;
		return true;
	}

	// ActiveConsole
	void GameDebug::ActiveConsole( const WString& name )
	{
		dict<WString,Console*>::iterator it = m_consoles.find( name );
		if( it == m_consoles.end() )
		{
			TRACE_ERROR( _S("GameDebug::ActiveConsole: Cannot find console '%s'.") , name.c_str() );
			return;
		}

		// change actived console
		if( m_activedConsole )
		{
			m_activedConsole->OnActive( false );

			if( m_activedConsole == it->second )
			{
				m_activedConsole = NULL;
				return;
			}
		}
		m_activedConsole = it->second;
		m_activedConsole->OnActive( true );
	}
	// GetActiveConsole
	Console * GameDebug::GetActiveConsole( void )
	{
		return m_activedConsole;
	}
	// Filter
	bool GameDebug::Filter( HWND hWnd , UINT message , WPARAM wParam , LPARAM lParam )
	{
		switch( message )
		{
		case WM_SIZE:
			dict<WString,Console*>::iterator it = m_consoles.begin();
			while( it!= m_consoles.end() )
			{
				++it;
			}
		}
		return false;
	}
	
} // namespace xeres
